package 集合;
import java.util.*;

public class X10对象一对多和多对多
{
	public static void main(String []args){
		//一对多实例
		Teacher t1=new Teacher("刘老师","男",25);
		Student s1=new Student("小明","男",12);
		Student s2=new Student("小li","男",10);
		Student s3=new Student("小花","女",13);
		
		t1.getStudents().add(s1);
		t1.getStudents().add(s2);
		t1.getStudents().add(s3);
		
		s1.setTeacher(t1);
		s2.setTeacher(t1);
		s3.setTeacher(t1);
		System.out.println(t1.getName());
		for(Student s:t1.getStudents()){
			System.out.println("\t|--"+s.getSname());
		}
		
		System.out.println("====================");
		
		//多对多实例
        Admin a1 = new Admin(1,"Admin");
        Admin a2 = new Admin(2,"Admin");
        Admin a3 = new Admin(3,"Admin");
        Role r1 = new Role(201,"信息整理员");
        Role r2 = new Role(302,"信息发布员");
        Role r3 = new Role(4,"信息审核");
        Group g1 = new Group(561,"信息管理");
        Group g2 = new Group(661,"用户管理");
        Group g3 = new Group(161,"数据管理");
        Group g4 = new Group(761,"接口管理");
        Group g5 = new Group(461,"备份管理");
        Action a11 = new Action(25,"新闻发布1","-");
        Action a12 = new Action(25,"新闻发布10","-");
        Action a13 = new Action(25,"新闻发布14","-");
        Action a14 = new Action(25,"新闻发布41","-");
        Action a15 = new Action(25,"新闻发布17","-");
        Action a16 = new Action(25,"新闻发布15","-");
        Action a17 = new Action(25,"新闻发布155","-");
        Action a18 = new Action(25,"新闻发布122","-");
        Action a19 = new Action(25,"新闻发布12222","-");
        Action a20 = new Action(25,"新闻发布12422","-");
        Action a21 = new Action(25,"新闻发布1252","-");
        Action a22 = new Action(25,"新闻发布1242","-");
        Action a23 = new Action(25,"新闻发布122222","-");
        //管理员与角色
        a1.setRole(r1);
        a2.setRole(r2);
        a3.setRole(r3);
        r1.setAdmins(new Admin[]{a1,a2});
        r2.setAdmins(new Admin[]{a1,a3});
        r3.setAdmins(new Admin[]{a2,a3});
        //角色和权限组
        r1.setGtoups(new Group[]{g1,g2});
        r2.setGtoups(new Group[]{g3,g4});
        r3.setGtoups(new Group[]{g5,g4});
        g1.setRoles(new Role[]{r1,r2});
        g2.setRoles(new Role[]{r1,r3});
        g3.setRoles(new Role[]{r2,r3});
        g4.setRoles(new Role[]{r2,r3});
        g5.setRoles(new Role[]{r1,r3});
        //权限组和权限
        a11.setGoup(g1);
        a12.setGoup(g3);
        a13.setGoup(g3);
        a14.setGoup(g1);
        a15.setGoup(g2);
        a16.setGoup(g4);
        a17.setGoup(g3);
        a18.setGoup(g5);
        a19.setGoup(g2);
        a20.setGoup(g5);
        a21.setGoup(g3);
        a22.setGoup(g4);
        a23.setGoup(g1);
        g1.setActions(new Action[]{a11,a12,a13});
        g2.setActions(new Action[]{a14,a15,a16});
        g3.setActions(new Action[]{a17,a18,a19});
        g4.setActions(new Action[]{a20,a21,a22});
        g5.setActions(new Action[]{a17,a12,a23});
        //
        System.out.println(a1.getInfo());
        System.out.println("\t|--"+a1.getRole().getInfo());
        for(int x=0;x<a1.getRole().getGroups().length;x++){
            System.out.println("\t\t\t|--"+a1.getRole().getGroups()[x].getInfo());
            for(int y=0;y<a1.getRole().getGroups()[x].getActions().length;y++){
                System.out.println("\t\t\t\t|--"+a1.getRole().getGroups()[x].getActions()[y].getInfo());
			}
        }
        System.out.println("------------------------------------------------------------------------------------");
        System.out.println(g1.getInfo());
        for(int a=0;a<g1.getRoles().length;a++){
            System.out.println("\t|--"+g1.getRoles()[a].getInfo());
            for(int d=0;d<g1.getRoles()[a].getAdmins().length;d++){
                System.out.println("\t\t\t|--"+g1.getRoles()[a].getAdmins()[d].getInfo());
            }
        }
    }
}
class Admin{
    private int id;
    private String password;
    private Role role;
    public Admin(int id,String password){
        this.id = id;
        this.password = password;
    }
    public void setRole(Role role){
        this.role = role;
    }
    public Role getRole(){
        return this.role;
    }
    public String getInfo(){
        return "管理员ID："+this.id+" "+"管理员密码："+this.password;
    }
}
class Role{
    private int rid;
    private String rname;
    private Admin []admins;
    private Group [] groups;
    public Role(int rid,String rname){
        this.rid = rid;
        this.rname = rname;
    }
    public void setGtoups(Group [] groups){
        this.groups = groups;
    }
    public Group [] getGroups(){
        return this.groups;
    }
    public void setAdmins(Admin [] admins){
        this.admins = admins;
    }
    public Admin [] getAdmins(){
        return this.admins;
    }
    public String getInfo(){
        return "角色ID："+this.rid+" "+"角色名称："+this.rname;
    }
}
class Group{
    private int gid;
    private String gname;
    private Role [] roles;
    private Action [] actions;
    public Group(int gid,String gname){
        this.gid = gid;
        this.gname = gname;
    }
    public void setActions(Action [] actions){
        this.actions = actions;
    }
    public Action [] getActions(){
        return this.actions;
    }
    public void setRoles(Role [] roles){
        this.roles = roles;
    }
    public Role []  getRoles(){
        return this.roles;
    }
    public String getInfo(){
        return "权限组ID："+this.gid+" "+"权限组名称："+this.gname;
    }
}
class Action{
    private int aid;
    private String aname;
    private String url;
    private Group group;
    public Action(int aid,String aname,String url){
        this.aid = aid;
        this.aname = aname;
        this.url = url;
    }
    public void setGoup(Group group){
        this.group = group;
    }
    public Group getGroup(){
        return this.group;
    }
    public String getInfo(){
        return "权限ID："+this.aid+" "+"权限名称："+this.aname+" "+"权限路径："+this.url;
    }
}
//-------------一对多--------------
class Student{
	private String sname;
	private String sex;
	private int age;
	private Teacher teacher;

	public Student(String sname, String sex, int age)
	{
		this.sname = sname;
		this.sex = sex;
		this.age = age;
	}

	public void setSname(String sname)
	{
		this.sname = sname;
	}

	public String getSname()
	{
		return sname;
	}

	public void setTeacher(Teacher teacher)
	{
		this.teacher = teacher;
	}

	public Teacher getTeacher()
	{
		return teacher;
	}
}
class Teacher{
	private String name;
	private String sex;
	private int age;
	private HashSet<Student> students=new HashSet<>();

	public Teacher(String name, String sex, int age)
	{
		this.name = name;
		this.sex = sex;
		this.age = age;
	}

	public void setStudents(HashSet<Student> students)
	{
		this.students = students;
	}

	public HashSet<Student> getStudents()
	{
		return students;
	}
	public String getName(){
		return name;
	}
	
}
